//防抖
export const debounce = (func, time = 500) => {
	if(!isFunction(func)||isNaN(time)) return;
	let timer = null;
	return () => {
		clearTimeout(timer);
		timer = setTimeout(() => {
			func.apply(this, arguments)
		}, time);
	}
}
//节流
export const throtte = (func, time = 500) => {
	if(!isFunction(func)||isNaN(time)) return;
	let activeTime = 0;
	return () => {
		const current = Date.now();
		if (current - activeTime > time) {
			func.apply(this, arguments);
			activeTime = Date.now();
		}
	}
}

/**
 * 时间格式化
 */
export const formateDate = (fmt, date)=>{
	if(!isDef(date)) return "";
	date = new Date(date);
    const o = {
        "M+" : date.getMonth()+1,                 //月份
        "D+" : date.getDate(),                    //日
        "H+" : date.getHours(),                   //小时
        "m+" : date.getMinutes(),                 //分
        "S+" : date.getSeconds(),                 //秒
        "Q+" : Math.floor((date.getMonth()+3)/3), //季度
        "s"  : date.getMilliseconds()             //毫秒
      };
    
      if(/(Y+)/.test(fmt)){
        fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
      }
      for(var k in o){
        if(new RegExp("("+ k +")").test(fmt)){
          fmt = fmt.replace(
            RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));  
        }       
      }
    
      return fmt;
}

/**
 * 转换rpx
 * 通过 :style=""这样的方式设置rpx像素值不会经过编译器，需调用方法做转换
 * 转换为以卡片跨度为基准的rpx值
 * @param {Number} pixel 设计稿对应rpx  
 * @return {String}
*/
export const convertCardRpx = (pixel)=>{
	pixel = parseFloat(pixel);
	if(!isDef(pixel)||isNaN(pixel)) return 0;
	return pixel*750/718+'rpx';
}

//定义
export const isDef = (val) =>{
	return val !== undefined && val !== null;
}
//函数
export const isFunction = (func) =>{
	return typeof func === 'function';
}
//对象
export const isObject = (val) =>{
	return val !== null && typeof val === 'object';
}
//Promise
export const isPromise = (val) =>{
	return isObject(val) && isFunction(val.then);
}

/*	兼容性检查
 * @param {String} name 名称
 * @return {Boolean}
*/
export const canIUse = (name) =>{
	if(!isDef(name)||name==="") return false;
	return weex.supports(name);
}